﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using University.Workshop.Business;

namespace University.Workshop.Presenters.WaterPurchase
{
    public class PresenterRegisterNewWaterPurchaseView
    {
        IRegisterBasicView viewRegisterBasic;
        Model.WaterPurchase newWaterPurchase;
        IRegisterNewWaterPurchaseView view;


        public PresenterRegisterNewWaterPurchaseView(IRegisterNewWaterPurchaseView View)
        {
            view = View;
            newWaterPurchase = new Model.WaterPurchase(); 
        }


        public void GoStepTwo()
        {
            revisarPlaca();


            view.RegisterBasic.ChargeDataToBasic();
            newWaterPurchase = view.RegisterBasic.WaterPurchaseToRegister;
            if (!PassFirstStep())
                view.CallFirstPage();
            else
                view.CallSecondPage();
            ProcedureAfterCall();
        }

        public void RegisterWaterPurchase()
        {
            revisarPlaca();

            view.RegisterBasic.ChargeDataToBasic();
            newWaterPurchase = view.RegisterBasic.WaterPurchaseToRegister;
            if (!PassFirstStep())
            {
                view.CallFirstPage();
                ProcedureAfterCall();
            }
            else if (!PassSecondStep())
            {
                view.CallSecondPage();
                ProcedureAfterCall();
            }
            else
            {
                ManagerWaterPurchase.NewWaterPurchase(newWaterPurchase);
                view.SystemMessage("La compra fue registrada exitosamente.");
                view.RestartRegisterWaterPurchase();
            }
        }

        private bool PassFirstStep()
        {
            try
            {
                Model.WaterPurchase wp = new Model.WaterPurchase();

                wp.LicencePlate = view.RegisterBasic.WaterPurchaseToRegister.LicencePlate;

                Model.Cistern cistern = ManagerWaterPurchase.GetCisternByPlate(new Model.Cistern { LicencePlate = wp.LicencePlate });
                
                //En caso de que no se haya presionado Enter
                view.RegisterBasic.WaterPurchaseToRegister.Capacity = cistern.Capacity.ToString();
                view.RegisterBasic.WaterPurchaseToRegister.PersonLastNames = cistern.LastNameDriver;
                view.RegisterBasic.WaterPurchaseToRegister.PersonNames = cistern.NameDriver;

                wp.Capacity = view.RegisterBasic.WaterPurchaseToRegister.Capacity;
                ManagerWaterPurchase.VerifyDataLicencePlate(wp);

                return true;
            }
            catch (Exception ex)
            {
                view.ErrorMessage(string.Format(ex.Message, "Registrar"));
                return false;
            }
        }

        private bool PassSecondStep()
        {
            try
            {
                Model.WaterPurchase wp = new Model.WaterPurchase();

                wp.DatePurchase = view.RegisterBasic.WaterPurchaseToRegister.DatePurchase;
                wp.Amount = view.RegisterBasic.WaterPurchaseToRegister.Amount;
                wp.TotalPrice = view.RegisterBasic.WaterPurchaseToRegister.TotalPrice;
                wp.Courtesy = view.RegisterBasic.WaterPurchaseToRegister.Courtesy;

                wp.DateRegistry = DateTime.Now;
                wp.Capacity = view.RegisterBasic.WaterPurchaseToRegister.Capacity;

                ManagerWaterPurchase.VerifyDataPurchase(wp);
                return true;
            }
            catch (Exception ex)
            {
                view.ErrorMessage(string.Format(ex.Message, "Registrar"));
                return false;
            }
        }

        public void GoBackPage()
        {
            viewRegisterBasic.GoBackPage();
        }

        public void revisarPlaca()
        {
            /*try
            {
                Model.WaterPurchase wp = new Model.WaterPurchase();

                wp.LicencePlate = view.RegisterBasic.WaterPurchaseToRegister.LicencePlate;
                ManagerWaterPurchase.VerifyDataLicencePlate(wp);
            }
            catch (Exception ex)
            {
                view.ErrorMessage(ex.Message);
            }*/
        }

        public void GoNextPage()
        {
            revisarPlaca();

            viewRegisterBasic.GoNextPage();
        }
        public void GoStepOne()
        {
            view.RegisterBasic.ChargeDataToBasic();
            newWaterPurchase = view.RegisterBasic.WaterPurchaseToRegister;
            view.CallFirstPage();
            ProcedureAfterCall();
        }

        private void ProcedureAfterCall()
        {
            if (viewRegisterBasic != null)
                newWaterPurchase = viewRegisterBasic.WaterPurchaseToRegister;
            viewRegisterBasic = view.RegisterBasic;
            viewRegisterBasic.WaterPurchaseToRegister = newWaterPurchase;
            viewRegisterBasic.Update();
        }

        public void FirstTime()
        {
            view.CallFirstPage();
            ProcedureAfterCall();
        }

        public void NotifyErrorMessage(string MessageValue)
        {
            view.ErrorMessage(MessageValue);
        }
        public void NotifySystemMessage(string MessageValue)
        {
            view.SystemMessage(MessageValue);
        }
    }
}
